package com.vanysoft.business.data.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.vanysoft.business.data.entity.DunsElementary;
import com.vanysoft.business.data.entity.DunsLink;
import com.vanysoft.business.data.entity.DunsUltimate;
import com.vanysoft.business.data.service.DunsElementaryService;
import com.vanysoft.business.data.service.DunsLinkService;
import com.vanysoft.business.data.service.DunsUltimateService;
import com.vanysoft.business.sys.entity.UserInfo;
import com.vanysoft.framework.aop.VanySoftValidation;
import com.vanysoft.framework.aop.VanySoftValidationException;
import com.vanysoft.framework.base.action.BaseController;
import com.vanysoft.framework.config.mvc.SessionUser;
import com.vanysoft.framework.utils.JSONResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Date;

/**
 * <p>
 * 供应商集团邓白氏编码关联 前端控制器
 * </p>
 *
 * @author YangLei
 * @since 2018-07-16
 */
@Controller
@RequestMapping("/data/duns")
public class DunsLinkController extends BaseController {

    @Autowired
    DunsLinkService dunsLinkService;
    @Autowired
    DunsUltimateService dunsUltimateService;
    @Autowired
    DunsElementaryService dunsElementaryService;

    @RequestMapping("index")
    public String index(HttpServletRequest request) {
        return "data/dunsCode";
    }

    @RequestMapping("list")
    @ResponseBody
    public JSONResponse list(Page<DunsLink> page, String ultimate, String elementary, String[] categories) {
        page = dunsLinkService.selectPage(page, ultimate, elementary, categories);
        return new JSONResponse(page);
    }

    @RequestMapping("save")
    @ResponseBody
    @VanySoftValidation
    public JSONResponse save(@Valid DunsLink dunsLink, BindingResult result, HttpServletRequest request, @SessionUser UserInfo userInfo) {
        Wrapper<DunsLink> wrapper = new EntityWrapper<DunsLink>().eq("is_enable", "1")
                .andNew().eq("ultimate_code", dunsLink.getUltimateCode())
                .or().eq("elementary_code", dunsLink.getElementaryCode());
        DunsLink entity = dunsLinkService.selectOne(wrapper);
        if (entity != null) {
            throw new VanySoftValidationException(getMessage("duns.hint.exist"));
        }
        dunsLink.setCreateTime(new Date());
        dunsLink.setModifyTime(new Date());
        dunsLink.setCreateUser(userInfo.getId());
        dunsLink.setModifyUser(userInfo.getId());
        dunsLinkService.insert(dunsLink);
        DunsUltimate dunsUltimate = dunsLink.getDunsUltimate();
        dunsUltimate.setUltimateCode(dunsLink.getUltimateCode());
        dunsUltimate.setCreateTime(new Date());
        dunsUltimate.setCreateUser(userInfo.getId());
        dunsUltimateService.insert(dunsUltimate);
        DunsElementary dunsElementary = dunsLink.getDunsElementary();
        dunsElementary.setElementaryCode(dunsLink.getElementaryCode());
        dunsElementary.setCreateTime(new Date());
        dunsElementary.setCreateUser(userInfo.getId());
        dunsElementaryService.insert(dunsElementary);
        return new JSONResponse();
    }

    @RequestMapping("update")
    @ResponseBody
    @VanySoftValidation
    public JSONResponse update(@Valid DunsLink dunsLink, BindingResult result, HttpServletRequest request, @SessionUser UserInfo userInfo) {
        dunsLink.setModifyTime(new Date());
        dunsLink.setModifyUser(userInfo.getId());
        dunsLinkService.updateById(dunsLink);
        dunsUltimateService.updateById(dunsLink.getDunsUltimate());
        dunsElementaryService.updateById(dunsLink.getDunsElementary());
        return new JSONResponse();
    }

    @RequestMapping("delete")
    @ResponseBody
    public JSONResponse delete(String id) {
        //TODO 该Elementary与供应商信息存在关联关系
        DunsLink dunsLink = dunsLinkService.selectById(id);
        dunsLink.setIsEnable("0");
        dunsLinkService.updateById(dunsLink);
        DunsUltimate dunsUltimate = dunsUltimateService.selectOne(new EntityWrapper<DunsUltimate>().eq("ultimate_code", dunsLink.getUltimateCode()));
        dunsUltimate.setIsEnable("0");
        dunsUltimateService.updateById(dunsUltimate);
        DunsElementary dunsElementary = dunsElementaryService.selectOne(new EntityWrapper<DunsElementary>().eq("elementary_code", dunsLink.getElementaryCode()));
        dunsElementary.setIsEnable("0");
        dunsElementaryService.updateById(dunsElementary);
        return new JSONResponse();
    }

    /**
     * 查询关联的所有品类
     *
     * @return
     */
    @RequestMapping("category")
    @ResponseBody
    public JSONResponse category() {
        return new JSONResponse(dunsLinkService.selectAllLinkCategory());
    }
}

